package Action;

import java.io.PrintWriter;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import Beans.Admin;
import Beans.PackageInfo;
import DAO.AdminDao;
import DAO.PackageInfoDao;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class APIAction extends ActionSupport {

	private String message;
	private String loginName;
	private String loginPsd;
	private String funId;
	private String parcelID;
	private PrintWriter writer;

	private List<Admin> admins;
	private List<PackageInfo> packageInfos;

	AdminDao adminDao = new AdminDao();
	PackageInfoDao packageInfoDao = new PackageInfoDao();

	/**
	 * API接口
	 * http://localhost/YouBoxAdmin/adminLogin?loginName=admin&loginPsd=admin
	 * &funId=6666
	 * http://localhost/YouBoxAdmin/parcelIdTomobileNum?parcelID=728150815547
	 * &funId=8888
	 * 
	 * @return
	 * @throws Exception
	 */
	public String fuckYou() throws Exception {
		writer = ServletActionContext.getResponse().getWriter();
		if ("6666".equals(funId)) {
			message = adminLogin();
		} else if ("8888".equals(funId)) {
			message = parcelIdTomobileNum();
		} else {
			message = "null";
		}
		writer.write(message);
		return null;
	}

	/**
	 * 货架管理员登陆
	 * 
	 * @return
	 * @throws Exception
	 */
	public String adminLogin() {
		admins = adminDao.adminLogin(loginName, loginPsd);
		if (admins.size() == 1) {
			message = "success:1";
		} else {
			message = "error:-1";
		}
		return message;
	}

	/**
	 * 根据运单号+验证码返回快递包裹的集合
	 * 
	 * @return
	 * @throws Exception
	 */
	public String parcelIdTomobileNum() {
		packageInfos = packageInfoDao.parcelIdTomobileNum(parcelID);
		if (packageInfos.size() == 1) {
			message = "success:" + packageInfos.get(0).getConsigneePhoneNum();
		} else {
			message = "error:-1";
		}
		return message;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getLoginPsd() {
		return loginPsd;
	}

	public void setLoginPsd(String loginPsd) {
		this.loginPsd = loginPsd;
	}

	public String getFunId() {
		return funId;
	}

	public void setFunId(String funId) {
		this.funId = funId;
	}

	public String getParcelID() {
		return parcelID;
	}

	public void setParcelID(String parcelID) {
		this.parcelID = parcelID;
	}

	public PrintWriter getWriter() {
		return writer;
	}

	public void setWriter(PrintWriter writer) {
		this.writer = writer;
	}

	public List<Admin> getAdmins() {
		return admins;
	}

	public void setAdmins(List<Admin> admins) {
		this.admins = admins;
	}

	public List<PackageInfo> getPackageInfos() {
		return packageInfos;
	}

	public void setPackageInfos(List<PackageInfo> packageInfos) {
		this.packageInfos = packageInfos;
	}

}
